<?php

namespace app\modules\admin\controllers;

use Yii;
use common\models\SysSource;
// use app\models\search\SysSourceSearch;

//use yii\web\Controller;
 use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Tools;

use app\components\AdminController;

/**
 * SysSourceController implements the CRUD actions for SysSource model.
 */
class SysSourceController extends AdminController
{
	public $enableCsrfValidation=false;
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
            ],
        ];
    }

    /**
     * admin/sys-source/index
     * Lists all SysSource models.
     * @return mixed
     */
    public function actionIndex()
    {
        $page_no = Tools::GetSafeParam('page_no', 1, 0);
        $condition = [];
        $condition['name'] = Tools::GetSafeParam('name', '', 1);
        
        $data = SysSource::getSourceList($condition,$page_no);
        
        $page = Tools::showCmPager($page_no, $data['perpage'], $data['total'],$condition);
        
        return $this->render('index',[
            'data' => $data,
            'page' => $page,
            'condition' => $condition,
        ]);
    }

    /**
     * admin/sys-source/view
     * Displays a single SysSource model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * admin/sys-source/create
     * Creates a new SysSource model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $name = Tools::GetSafeParam('name', '', 1);
        $parent = Tools::GetSafeParam('parent', 0, 0);
        $ismenu = Tools::GetSafeParam('ismenu', 0, 0);
        $url = Tools::GetSafeParam('url', '', 1);
        
        if($name != ""){
            $model = new SysSource();
            $model->name = $name;
            $model->parent = $parent;
            $model->ismenu = $ismenu;
            $model->url = $url;
            $model->addtime = time();
            if($model->save()){
                return $this->redirect(['index']);
            }
        }
        
        $top_source = SysSource::getTopSources();

        return $this->render('create',['top_source'=>$top_source]);
    }

    /**
     * admin/sys-source/update
     * Updates an existing SysSource model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate()
    {
        $id = Tools::GetSafeParam('id', 0, 0);
        $model = $this->findModel($id);
        $top_source = SysSource::getTopSources();
        
        $name = Tools::GetSafeParam('name', '', 1);
        $parent = Tools::GetSafeParam('parent', 0, 0);
        $ismenu = Tools::GetSafeParam('ismenu', 0, 0);
        $url = Tools::GetSafeParam('url', '', 1);
        
        if($name!=""){
            $model->name = $name;
            $model->parent = $parent;
            $model->url = $url;
            $model->ismenu = $ismenu;
            if($model->save()){
                return $this->redirect(['index']);
            }
        }
        
        return $this->render('update', [
            'model' => $model,
            'top_source'=>$top_source
        ]);
    }
    
//    public function actionUpdateSave()
//    {
//        $name = Tools::GetSafeParam('name', '', 1);
//        $id = Tools::GetSafeParam('id', 0, 0);
//        $parent = Tools::GetSafeParam('parent', 0, 0);
//        $ismenu = Tools::GetSafeParam('ismenu', 0, 0);
//        $url = Tools::GetSafeParam('url', '', 1);
//        $model = $this->findModel($id);
//        $model->name = $name;
//        $model->parent = $parent;
//        $model->url = $url;
//        $model->ismenu = $ismenu;
//        if($model->save()){
//            $output['success'] = 1;
//            $output['msg'] = '添加成功！';
//            Tools::outputjson($output);
//        }
//        else{
//            $output['success'] = 0;
//            $output['msg'] = '失败:'.$model->errors;
//            Tools::outputjson($output);
//        }
//        
//    }

    /**
     * admin/sys-source/delete
     * Deletes an existing SysSource model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        if($this->findModel($id)->delete()){
            $output['success'] = 1;
            $output['msg'] = "删除成功！";
            Tools::outputjson($output);
        }
        else{
            $output['success'] = 0;
            $output['msg'] = "删除失败！";
            Tools::outputjson($output);
        }
    }

    /**
     * Finds the SysSource model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return SysSource the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = SysSource::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
